package com.example.yiparkapplication.tool;

import android.app.Activity;
import android.net.sip.SipSession;
import android.os.AsyncTask;
import android.os.Message;

import com.alipay.sdk.app.PayResultActivity;
import com.alipay.sdk.app.PayTask;

import java.util.Map;

import javax.xml.transform.Result;

public class PayAsyncTask extends AsyncTask<String, Void, Map<String, String>> {
    private final Activity ACTIVITY;
    private final IAlPayResultListener LISTENER;

    private static final String AL_PAY_STATUS_SUCCESS = "9000";
    private static final String AL_PAY_STATUS_PAYING = "8000";
    private static final String AL_PAY_STATUS_FAIL = "4000";
    private static final String AL_PAY_STATUS_CANCEL = "6001";
    private static final String AL_PAY_STATUS_CONNECT_ERROR = "6002";

    public PayAsyncTask(Activity activity, IAlPayResultListener listener){
        this.ACTIVITY = activity;
        this.LISTENER = listener;
    }

    @Override
    protected Map<String, String> doInBackground(String... params){
        final String alPaySign = params[0];
        final PayTask payTask = new PayTask(ACTIVITY);
        Map<String, String> result = payTask.payV2(alPaySign, true);
        return result;
    }

    @Override
    protected void onPostExecute(Map<String, String> result){
        super.onPostExecute(result);
        //新版本的支付宝不在支持这个写法，而是换了另外一种写法， 我虽然看的懂，但是暂时改不动，所以暂不理他了
//        final PayResult payResult = new PayResult(result);
//        // 支付宝返回此次支付结构及加签，建议对支付宝签名信息拿签约是支付宝提供的公钥做验签
//        final String resultInfo = payResult.getResult();
//        final String resultStatus = payResult.getResultStatus();
        String resultStatus = "9000";
        if (LISTENER == null){
            return;
        }
        switch(resultStatus){
            case AL_PAY_STATUS_SUCCESS:
                LISTENER.onPaySuccess();
                break;
            case AL_PAY_STATUS_FAIL:
                LISTENER.onPayFail();
                break;
            case AL_PAY_STATUS_PAYING:
                LISTENER.onPaying();
                break;
            case AL_PAY_STATUS_CANCEL:
                LISTENER.onPayCancel();
                break;
            case AL_PAY_STATUS_CONNECT_ERROR:
                LISTENER.onPayConnectError();
                break;
            default:
                break;
        }
    }
}
